import { ref } from "vue";

// 管理步骤

export function useStep(steps: any[], defaultIndex = 0) {

  const nowStepIndex = ref(defaultIndex);
  const nowStep = ref(steps[nowStepIndex.value]);

  function next() {
    if (nowStepIndex.value < steps.length - 1) {
      nowStepIndex.value++;
      nowStep.value = steps[nowStepIndex.value]
    }
  }

  function back() {
    if (nowStepIndex.value > 0) {
      nowStepIndex.value--;
      nowStep.value = steps[nowStepIndex.value]
    }
  }

  function jump(tar: number) {
    if (tar >= 0 && tar <= steps.length - 1) {
      nowStepIndex.value = tar;
      nowStep.value = steps[nowStepIndex.value]
    }
  }

  return {
    nowStepIndex, nowStep, next, back, jump
  }
}